//
//  AUEssenceTopicViewModel.swift
//  Alamofire
//
//  Created by Miss_Special on 2018/4/17.
//

import UIKit
import AwesomeUNetworkModule

let AUEssenceTopicCell_identifier = "AUEssenceTopicCell_identifier"

class AUEssenceTopicViewModel: NSObject {
    
    fileprivate var dataArray = [AUEssenceTopicModel]()
    
    var requestUrl: String = ""
    
    var needRefresh: (() -> ())?

    func requestData() {
        AUHomeModule_NetwrokManager.sharedManager.request(.get, urlString: requestUrl) { (result) in
            guard let dictArray = result["list"] as? [[String: Any]] else {
                return
            }
            var tempArray = [AUEssenceTopicModel]()
            for dict in dictArray {
                tempArray.append(AUEssenceTopicModel(dict: dict))
            }
            self.dataArray = tempArray
            self.needRefresh?()
        }
    }
}

extension AUEssenceTopicViewModel {
    
    func registerCell(_ tableView: UITableView) {
//        let bundle = Bundle(for: type(of: self))
//        tableView.register(
//            UINib(nibName: "AUEssenceTopicCell", bundle: bundle),
//            forCellReuseIdentifier: "AUEssenceTopicCell"
//        )
        tableView.register(AUEssenceTopicCell.self, forCellReuseIdentifier: AUEssenceTopicCell_identifier)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> AUEssenceTopicCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: AUEssenceTopicCell_identifier, for: indexPath) as! AUEssenceTopicCell
        cell.model = dataArray[indexPath.row]
        return cell
    }
}
